package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;

import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.beans.Transient;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderDetailService orderDetailService;

    @PostMapping("/submit")
    public R<String>submit(@RequestBody Orders orders) {
        log.info("订单数据...{}", orders);
        orderService.submit(orders);
        return R.success("成功");
    }

    /**
     * 客户端订单分页
     * @param page
     * @param pageSize
     * @param number
     * @return
     */
    @Transient
    @GetMapping("/page")
    public R<Page> page( int page,int pageSize,Long number) {
        log.info("page={},pageSize={},number={}",page,pageSize,number);
        //构造分页构造器对象
        Page<Orders> pageInfo=new Page<>(page,pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Orders> queryWrapper=new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.eq(number!=null,Orders::getNumber,number);
        //添加排序条件orderByAsc升序,orderByDesc降序
        queryWrapper.orderByDesc(Orders::getCheckoutTime).orderByDesc(Orders::getCheckoutTime);
        //执行分页查询
        orderService.page(pageInfo, queryWrapper);

        return R.success(pageInfo);
    }

    /**
     * 修改订单状态
     * @return
     */
    @PutMapping("")
    public R<String> Stats( @RequestBody Orders orders) {
        log.info("dasfsd {}",orders);
        LambdaQueryWrapper<Orders> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getId,orders.getId());
        Orders one = orderService.getOne(queryWrapper);
        one.setStatus(orders.getStatus());
        orderService.updateById(one);
        return R.success("配送成功");
    }

    /**
     *移动端订单查询/分页
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> page(int page, int pageSize) {
        //获取当前id
        Long userId = BaseContext.getCurrentId();
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        Page<OrdersDto> ordersDtoPage = new Page<>(page, pageSize);
        //条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        //查询当前用户id订单数据
        queryWrapper.eq(userId != null, Orders::getUserId, userId);
        //按时间降序排序
        queryWrapper.orderByDesc(Orders::getOrderTime);
        orderService.page(pageInfo, queryWrapper);
        //拷贝除records的所有信息
        BeanUtils.copyProperties(pageInfo, ordersDtoPage, "records");
        List<OrdersDto> list = pageInfo.getRecords().stream().map((item) -> {
            OrdersDto ordersDto = new OrdersDto();
            //将orders的基础信息保存到ordersDto
            BeanUtils.copyProperties(item, ordersDto);
            //获取orderId,然后根据这个id，去orderDetail表中查数据
            Long orderId = item.getId();
            LambdaQueryWrapper<OrderDetail> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(OrderDetail::getOrderId, orderId);
            List<OrderDetail> details = orderDetailService.list(wrapper);
            //将orderDetail的基础信息保存到ordersDto
            ordersDto.setOrderDetails(details);
            return ordersDto;
        }).collect(Collectors.toList());
        ordersDtoPage.setRecords(list);
        //日志输出看一下
        log.info("list:{}", list);
        return R.success(ordersDtoPage);
    }


    /**
     * 删除订单
     * @param orders
     * @return
     */
    @DeleteMapping("/delete")
    public R<String> Delete(Orders orders) {
        log.info("id={}",orders);
        orderService.removeById(orders.getId());
        return R.success("删除成功");
    }



}
